﻿using System;
using System.Collections.Generic;
using System.Text;
using AO.Core;

namespace ZoneEngine.PacketHandlers
{
    class GenericCmd
    {
        static int _temp1, _count, _action, _temp4;
        static Identity _user, _target;
        static Client _sender;
        public static void Read(byte[] packet, Client client)
        {
            _sender = client;
            PacketReader reader = new PacketReader(packet);
            Header _header = reader.PopHeader();
            reader.PopByte();
            _temp1 = reader.PopInt();
            _count = reader.PopInt();    // Count of commands sent
            _action = reader.PopInt();
            _temp4 = reader.PopInt();
            _user = reader.PopIdentity();
            _target = reader.PopIdentity();
            reader.Finish();
            switch (_action)
            {
                case 1:
                    // Get
                    break;
                case 2:
                    // Drop
                    break;
                case 3:
                    // Use
                    OnUse();
                    break;
                case 4:
                    // Repair
                    break;
                case 5:
                    // UseItemOnItem
                    break;
                default:
                    break;
            }
            string Feedback1 = string.Format("T1 {0}, Count {1}, Action {2}, T4 {3}", _temp1, _count, _action, _temp4);
            string Feedback2 = string.Format("User {0}:{1}, Target {2}:{3}", _user.Type, _user.Instance, _target.Type, _target.Instance);
            client.SendChatText(Feedback1);
            client.SendChatText(Feedback2);
        }

        static void OnUse()
        {
            uint _t_instance = BitConverter.ToUInt32(BitConverter.GetBytes(_target.Instance), 0);

            //LuaInterface.LuaTable tbl = Program.Script.GetLua().GetTable("PlayfieldSettings");
            bool result = Program.Script.CallHook("OnUse", _sender.Character.pf, _sender, _target, _t_instance);
            if (result == true)
            {
                Reply();
            }

            /*switch (_sender.Character.pf)
            {
                case 500:
                    // Soldier
                    if ((_target.Type == 51005) && (_t_instance == 3222340084))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 1);
                            _sender.Character.stats.SetStat(368, 1);
                            Reply();
                        }
                    }
                    // Martial Artist
                    if ((_target.Type == 51005) && (_t_instance == 3222405620))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 2);
                            _sender.Character.stats.SetStat(368, 2);
                            Reply();
                        }
                    }
                    // Engineer
                    if ((_target.Type == 51005) && (_t_instance == 3222471156))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 3);
                            _sender.Character.stats.SetStat(368, 3);
                            Reply();
                        }
                    }
                    // Fixer 
                    if ((_target.Type == 51005) && (_t_instance == 3222536692))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 4);
                            _sender.Character.stats.SetStat(368, 4);
                            Reply();
                        }
                    }
                    // Agent
                    if ((_target.Type == 51005) && (_t_instance == 3222602228))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 5);
                            _sender.Character.stats.SetStat(368, 5);
                            Reply();
                        }
                    }
                    // Adventurer
                    if ((_target.Type == 51005) && (_t_instance == 3222667764))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 6);
                            _sender.Character.stats.SetStat(368, 6);
                            Reply();
                        }
                    }
                    // Trader
                    if ((_target.Type == 51005) && (_t_instance == 3222733300))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 7);
                            _sender.Character.stats.SetStat(368, 7);
                            Reply();
                        }
                    }
                    // Bureaucrat
                    if ((_target.Type == 51005) && (_t_instance == 3222798836))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 8);
                            _sender.Character.stats.SetStat(368, 8);
                            Reply();
                        }
                    }
                    // Enforcer
                    if ((_target.Type == 51005) && (_t_instance == 3222864372))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 9);
                            _sender.Character.stats.SetStat(368, 9);
                            Reply();
                        }
                    }
                    // Doctor
                    if ((_target.Type == 51005) && (_t_instance == 3222929908))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 10);
                            _sender.Character.stats.SetStat(368, 10);
                            Reply();
                        }
                    }
                    // Nano Techician
                    if ((_target.Type == 51005) && (_t_instance == 3222995444))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 11);
                            _sender.Character.stats.SetStat(368, 11);
                            Reply();
                        }
                    }
                    // Meta Phycisist
                    if ((_target.Type == 51005) && (_t_instance == 3223060980))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 12);
                            _sender.Character.stats.SetStat(368, 12);
                            Reply();
                        }
                    }
                    // Shade
                    if ((_target.Type == 51005) && (_t_instance == 3223323124))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 15);
                            _sender.Character.stats.SetStat(368, 15);
                            Reply();
                        }
                    }
                    // Keeper
                    if ((_target.Type == 51005) && (_t_instance == 3223388660))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(60, 14);
                            _sender.Character.stats.SetStat(368, 14);
                            Reply();
                        }
                    }
                    // Neutral
                    if ((_target.Type == 51005) && (_t_instance == 3223126516))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(33, 0);
                            Reply();
                        }
                    }
                    // Clan
                    if ((_target.Type == 51005) && (_t_instance == 3223192052))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(33, 1);
                            Reply();
                        }
                    }
                    // Omni-Tek
                    if ((_target.Type == 51005) && (_t_instance == 3223257588))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(33, 2);
                            Reply();
                        }
                    }

                    // Nano Male
                    if ((_target.Type == 51005) && (_t_instance == 3221553652))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(367, 3);
                            _sender.Character.stats.SetStat(369, 2);
                            _sender.Character.stats.SetStat(59, 2);
                            _sender.Character.stats.SetStat(4, 3);
                            Reply();
                        }
                    }
                    // Nano Female
                    if ((_target.Type == 51005) && (_t_instance == 3221619188))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(367, 3);
                            _sender.Character.stats.SetStat(369, 3);
                            _sender.Character.stats.SetStat(59, 3);
                            _sender.Character.stats.SetStat(4, 3);
                            Reply();
                        }
                    }
                    // Atrox
                    if ((_target.Type == 51005) && (_t_instance == 3221684724))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(367, 4);
                            _sender.Character.stats.SetStat(369, 1);
                            _sender.Character.stats.SetStat(59, 1);
                            _sender.Character.stats.SetStat(4, 4);
                            Reply();
                        }
                    }
                    // Opifex Female
                    if ((_target.Type == 51005) && (_t_instance == 3221488116))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(367, 2);
                            _sender.Character.stats.SetStat(369, 3);
                            _sender.Character.stats.SetStat(59, 3);
                            _sender.Character.stats.SetStat(4, 2);
                            Reply();
                        }
                    }
                    // Opifex Male
                    if ((_target.Type == 51005) && (_t_instance == 3221422580))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(367, 2);
                            _sender.Character.stats.SetStat(369, 2);
                            _sender.Character.stats.SetStat(59, 2);
                            _sender.Character.stats.SetStat(4, 2);
                            Reply();
                        }
                    }
                    // Solitus Female
                    if ((_target.Type == 51005) && (_t_instance == 3221357044))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(367, 1);
                            _sender.Character.stats.SetStat(369, 3);
                            _sender.Character.stats.SetStat(59, 3);
                            _sender.Character.stats.SetStat(4, 1);
                            Reply();
                        }
                    }
                    // Solitus Male
                    if ((_target.Type == 51005) && (_t_instance == 3221291508))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(367, 1);
                            _sender.Character.stats.SetStat(369, 2);
                            _sender.Character.stats.SetStat(59, 2);
                            _sender.Character.stats.SetStat(4, 1);
                            _sender.Character.stats.SetStat(58, 99);
                            Reply();
                        }
                    }
                    // Ectomorph
                    if ((_target.Type == 51005) && (_t_instance == 3222077940))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(47, 0);
                            Reply();
                        }
                    }
                    // Mesomorph
                    if ((_target.Type == 51005) && (_t_instance == 3221946868))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(47, 1);
                            Reply();
                        }
                    }
                    // Endomorph
                    if ((_target.Type == 51005) && (_t_instance == 3222012404))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(47, 2);
                            Reply();
                        }
                    }
                    // Tall
                    if ((_target.Type == 51005) && (_t_instance == 3222209012))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(360, 110);
                            Reply();
                        }
                    }
                    // Normalize
                    if ((_target.Type == 51005) && (_t_instance == 3222143476))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(360, 100);
                            Reply();
                        }
                    }
                    // Short
                    if ((_target.Type == 51005) && (_t_instance == 3222274548))
                    {
                        if (_sender.Character.stats.GetStat("GmLevel") > 0)
                        {
                            _sender.Character.stats.SetStat(360, 90);
                            Reply();
                        }
                    }
                    break;
                default:
                    break;
            }*/
        }

        internal static void Reply()
        {
            PacketWriter writer = new PacketWriter();
            writer.PushBytes(new byte[] { 0xDF, 0xDF });
            writer.PushShort(10);
            writer.PushShort(1);
            writer.PushShort(0);
            writer.PushInt(3086);
            writer.PushInt(_sender.Character.characterId);
            writer.PushInt(0x52526858);
            writer.PushIdentity(_user);
            writer.PushByte(0);
            writer.PushInt(1);
            writer.PushInt(_count);
            writer.PushInt(_action);
            writer.PushInt(_temp4);
            writer.PushIdentity(_user);
            writer.PushIdentity(_target);
            byte[] reply = writer.Finish();
            _sender.CmpStream.Write(reply, 0, reply.Length);
            _sender.CmpStream.Flush();
        }
    }
}
